"""author: wenyao
   data: 2021/4/16
   project: devopscmdb
"""
#遇到错误，程序就终止了
# print("start.......")
# f = open("test.txt")
# print("end..........")

#常见异常
#NameError
# print(abc)

#ZeroDivisionError
# print(1/0)

#IndentationError
 # print("ab")

#SyntaxError
# if 1>0 ::
#    print("ok")

#IndexError
# a = [1,2,3]
# print(a[3])

#KeyError
# a = {"a":1,"b":2}
# print(a["c"])
# print(a.get("c",0))

#AttributeError
# import random
# random.xxx()

#ValueError
# int("abc")


######异常机制
#try... except...
#好处：异常发生，不终止程序； 异常发生，做特殊处理
#except捕获的错误与成语触发的错误不一致，程序就捕获不到，然后抛出异常报错
#except 可以有多个
#except 匹配到第一个就退出
#如果父类异常放在最前面，就会吞噬所有的子类异常
#
# try:
#     print("start try ....")
#     a = 1/0
#     # int("abc")
#     # a = [1,2]
#     # print(a[3])
#     print("end try.....")
# except ZeroDivisionError as ze:
#     print("zero error:",ze)
# except ValueError as ve:
#     print("value error:",ve)
# except Exception as e:
#     print("exception:",e)
#
# print("try ....test")

# try:
#     a = 1/0
# except:
#     print("error")


#else结构
#没有任何异常发生的时候执行的
try:
    print("else test ....")
   # a = 1/0
except:
    print("error")
else:
    print("else test end.....")

print("end.....")

#finally
#不管有没有异常，不管异常有没有被捕获都执行
# try:
#     print("finally test....")
#     a = 1/0
# except ValueError:
#     print("error")
# else:
#     print("else test....")
# finally:
#     print("finally test ending.....")
#
# print("end.........")
#
# def finally_test():
#     try:
#         print("try test........")
#         try:
#             int("abc")
#         except ValueError as e:
#             return "i am value error"
#         finally:
#             print("i am value error finally")
#         a = 1/0
#     except ZeroDivisionError:
#         print("zero eror")
#     finally:
#         print("i am zero error finally")
# ft = finally_test()
# print(ft)

#手动触发异常
#raise
#raise 异常类
#raise 异常类("说明信息")
#raise没有接异常类，默认抛出RuntimeError的异常
# try:
#     print("raise error")
#     raise
#     raise IndexError("this is index error")
# except RuntimeError as re:
#     print("raise error",re)
# except IndexError as ie:
#     print("raise index error:",ie)
